/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Database_Control;

import Database_Control.Model.t_KhoMaster;
import com.sun.rowset.CachedRowSetImpl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.rowset.CachedRowSet;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

/**
 *
 * @author BoiDell
 */
public class KhoMasterControl {
    public static CachedRowSet crs;
    Connection conn = null;
    String jdbcURL = "jdbc:mysql://localhost/quanlykho_java";
    String jdbcDriver = "com.mysql.jdbc.Driver";
    String user = "root";
    String password = "";
    
    public KhoMasterControl(){
        try {
            DbUtils.loadDriver(jdbcDriver);
            conn = DriverManager.getConnection(jdbcURL, user, password);
        } catch (SQLException ex) {
            Logger.getLogger(KhoMasterControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    public List<t_KhoMaster> GetAll(){
        QueryRunner qRunner = new QueryRunner();
        List<t_KhoMaster> result = new ArrayList<t_KhoMaster>();
        try {
            String sql = "select * from t_KhoMaster order by CreatedTime";
            List mapList = (List) qRunner.query(conn, sql, new MapListHandler());
            for (int i = 0; i < mapList.size(); i++) {
                Map map = (Map) mapList.get(i);
                t_KhoMaster kho = new t_KhoMaster();
                kho.setID((Long)map.get("ID"));
                kho.setTen((String)map.get("Ten"));
                kho.setActive((Boolean)map.get("Active"));
                kho.setCreatedTime((java.sql.Date)map.get("CreatedTime"));
                result.add(kho);
            }
        } catch (SQLException ex) {
            Logger.getLogger(KhoMasterControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    
    public int InsertKho(t_KhoMaster KhoMaster){
        int newID = 0;
        try {
            int active = KhoMaster.isActive() ? 1 : 0;
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            String sql = "Insert into t_KhoMaster(Ten,Active,CreatedTime) values('"+KhoMaster.getTen()+"','"+active+"','"+dateFormat.format(date)+"')";
            QueryRunner qRunner = new QueryRunner();
            newID = qRunner.update(conn,sql);
            
        } catch (SQLException ex) {
            Logger.getLogger(KhoMasterControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return newID;
    }
    
    public int UpdateKho (t_KhoMaster KhoMaster){
        int updateId = 0;
        try {
            int active = KhoMaster.isActive() ? 1 : 0;
            QueryRunner qRunner = new QueryRunner();
            updateId = qRunner.update(conn,"update t_KhoMaster set Ten = ?,Active = ? where ID = ?",KhoMaster.getTen(),active,KhoMaster.getID());
        } catch (SQLException ex) {
            Logger.getLogger(KhoMasterControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return updateId;
    }
    
    public int DeleteKho (long ID){
        int updateId = 0;
        try {
            QueryRunner qRunner = new QueryRunner();
            updateId = qRunner.update(conn,"delete from t_KhoMaster where ID = ?",ID);
        } catch (SQLException ex) {
            Logger.getLogger(KhoMasterControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return updateId;
    }
}
